package org.jeecg.modules.inspection.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.modules.inspection.entity.Inspection;
import org.jeecg.modules.inspection.entity.InspectionException;
import org.jeecg.modules.inspection.entity.InspectionTaskRank;
import org.jeecg.modules.inspection.vo.InspectionReportVO;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


/**
 * @Description: 巡检主表
 * @Author: jeecg-boot
 * @Date:   2019-09-26
 * @Version: V1.0
 */
@Repository
public interface InspectionMapper extends BaseMapper<Inspection> {

   Integer updateStatus(Inspection inspection);
   @SqlParser(filter = true)
   void insertBatch(@Param("infos") List<Inspection> singletonList);
   @Select("select * from set_inspection_exception where office_id = #{officeId} and recive_type=#{reciveType} and del_flag ='0'")
   InspectionException selectOfficeConfig(@Param("officeId")String officeId,@Param("reciveType")Integer reciveType);
   List<Inspection> selectAllItem(@Param("userId")String userId);
   List<Inspection> listByProject(@Param("page") Page<Inspection> pageList, @Param("inspection") Inspection inspection);

   @SqlParser(filter = true)
   @Select("SELECT id FROM process_inspection WHERE overtime_date < #{now} AND status in ('100','200') AND del_flag = 0")
   List<String> getOutTimeList(@Param("now") Date now);

   @SqlParser(filter = true)
   @Update("UPDATE process_inspection SET status = '500' WHERE overtime_date < #{now} AND status in ('100','200') AND del_flag = 0")
   Integer updateToOutTime(@Param("now") Date now);

   @Select("SELECT pia.id as areaId,pia.name as areaName,pia.status as areaStatusId,sdStatus.item_text as areaStatusName,pia.is_check_res as areaIsCheckRes\n" +
           " ,IFNULL(pii.id,'') as itemId,pii.name as itemName,pii.is_check as itemIsCheck,pii.is_check_res as itemIsCheckRes\n" +
           " ,pie.id as execId,pie.exce_explain as execExplain,pie.comfirm_res as execComfirmRes\n" +
           " FROM process_inspection_area pia\n" +
           " LEFT JOIN process_inspection_item pii on pii.process_area_id = pia.id AND  pii.is_check_res = '1' \n" +
           " LEFT JOIN process_inspection_exception pie on pie.process_item_id = pii.id\n" +
           " LEFT JOIN (SELECT sdi.item_text,sdi.item_value FROM sys_dict sd INNER JOIN sys_dict_item sdi on sd.id = sdi.dict_id WHERE sd.dict_code = 'inspection_status') sdStatus on sdStatus.item_value = pia.status\n" +
           " WHERE pia.process_id = #{processId} ORDER BY pia.id,pii.id")
   List<InspectionReportVO> queryReportByLine(@Param("processId")String processId);
   @Select("select * from set_inspection_exception where office_id = #{officeId} and del_flag='0'")
   List<InspectionException> queryConfig(@Param("officeId") String officeId);

    List<InspectionTaskRank> selectAllPlan(IPage page,InspectionTaskRank inspectionTaskRank);

   List<Inspection> selectPlanCountData(@Param("userId") String userId,  @Param("date") String date);
}
